package com.example.ai.recognition.activity;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;import com.google.gson.reflect.TypeToken;import com.example.ai.recognition.R;import com.example.ai.recognition.face.baidu.utils.GsonUtils;import com.example.ai.recognition.face.baidu.v3.FaceRecognitionManager;import com.example.ai.recognition.face.baidu.v3.Result;import com.example.ai.recognition.utils.SimpleLog;import org.json.JSONObject;import java.io.File;import java.io.UnsupportedEncodingException;import java.util.HashMap;import io.reactivex.Observable;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.schedulers.Schedulers;public class RecognitionActivity extends AppCompatActivity { SimpleLog log = new SimpleLog("yy"); static final TypeToken<HashMap<String, Object>> mapToken = new TypeToken<HashMap<String, Object>>() {}; final int TAKE_PHOTO_CODE = 111; final int TAKE_LOCALFILE_CODE = 112; RadioGroup livesRadioGroup, qualityRadioGroup; CheckBox ck_multi; ImageView imageview; TextView tv_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recognition); livesRadioGroup = findViewById(R.id.livesRadioGroup); qualityRadioGroup = findViewById(R.id.qualityRadioGroup); ck_multi = findViewById(R.id.ck_multi); tv_message = findViewById(R.id.tv_message);// tv_userid = findViewById(R.id.tv_userid);// tv_userinfo = findViewById(R.id.tv_userinfo);// tv_score = findViewById(R.id.tv_score); imageview = findViewById(R.id.imageview); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { log.d("onActivityResult requestCode = " requestCode ",resultCode = " resultCode); switch (requestCode) { case TAKE_PHOTO_CODE: if (resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); imageview.setImageBitmap(bitmap); recognition(bitmap); } else if (resultCode == RESULT_CANCELED) { log.d("用户取消了拍照"); } break; case TAKE_LOCALFILE_CODE: if (resultCode == RESULT_OK) { try { Uri selectedImage = data.getData(); //获取系统返回的照片的Uri String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片 cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String path = cursor.getString(columnIndex); //获取照片路径 cursor.close(); Bitmap bitmap = BitmapFactory.decodeFile(path); imageview.setImageBitmap(bitmap); recognition(path); } catch (Exception e) { // TODO Auto-generatedcatch block e.printStackTrace(); } } else if (resultCode == RESULT_CANCELED) { log.d("用户取消了相册选择"); } break; } } /** * 拍照识别按钮响应 * * @param view */ public void takePhoto(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; } Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //系统常量, 启动相机的关键 openCameraIntent.putExtra("camerasensortype", 1); // 调用前置摄像头 ,红米Note 5// openCameraIntent.putExtra("autofocus", true); // 自动对焦// openCameraIntent.putExtra("fullScreen", false); // 全屏// openCameraIntent.putExtra("showActionIcons", false); startActivityForResult(openCameraIntent, TAKE_PHOTO_CODE); // 参数常量为自定义的request code, 在取返回结果时有用 } /** * 测试,从本地直接读取图片 * * @param view */ public void takefile(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; }// String path = "/mnt/sdcard/ytest/yjz.jpg";// recognition(path); //在这里跳转到手机系统相册里面 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, TAKE_LOCALFILE_CODE); } public void test(View view) { if (TextUtils.isEmpty(FaceRecognitionManager.getInstance().getAccessToken())) { showToat("尚未获取到AccessToken"); return; } File f = new File("/mnt/sdcard/ytest/4ren.jpg"); log.d("文件是否存在:" f.exists()); recognition("/mnt/sdcard/ytest/4ren.jpg"); } /** * 识别 * * @param bitmap */ private void recognition(Bitmap bitmap) { tv_message.setText(""); String groupid = FaceRecognitionManager.getInstance().getGroupid(); log.d("尝试识别 groupid =" groupid ",getQualityControl =" getQualityControl() ",getLivesControl =" getLivesControl() ",bitmap.getByteCount =" bitmap.getByteCount() ",ck_multi.isChecked():" ck_multi.isChecked()); Observable<Result> observable = null; if (ck_multi.isChecked()) { log.d("多人识别"); try { observable = FaceRecognitionManager.getInstance().searchMultiUser(bitmap, groupid, getQualityControl(), getLivesControl()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } } else { log.d("单人识别"); observable = FaceRecognitionManager.getInstance().searchUserByBitmap(bitmap, groupid, getQualityControl(), getLivesControl()); } observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Result>() { @Override public void accept(Result result) throws Exception { if (result.isSuccess()) {// JSONObject obj = result.getResult();// String user = obj.getJSONArray("user_list").getString(0);// HashMap<String, Object> map = GsonUtils.fromJson(user, mapToken.getType());// tv_userid.setText(map.get("user_id").toString());// tv_userinfo.setText(map.get("user_info").toString());// tv_score.setText(map.get("score").toString());// tv_message.setText(result.getResponse()); } else { showToat("code:" result.getCode() ",msg:" result.getErrorMsg()); } tv_message.setText(result.getResponse()); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { throwable.printStackTrace(); showToat("异常错误"); } }); } private void recognition(String imgeFilePath) { tv_message.setText(""); final long start = System.currentTimeMillis(); String groupid = FaceRecognitionManager.getInstance().getGroupid(); log.d("尝试识别 groupid =" groupid ",imgeFilePath=" imgeFilePath ",getQualityControl =" getQualityControl() ",getLivesControl =" getLivesControl()); Observable<Result> observable = null; if (ck_multi.isChecked()) { log.d("多人识别"); try { observable = FaceRecognitionManager.getInstance().searchMultiUser(imgeFilePath, groupid, getQualityControl(), getLivesControl()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } } else { log.d("单人识别"); observable = FaceRecognitionManager.getInstance().searchUserByImageFile(imgeFilePath, groupid, getQualityControl(), getLivesControl()); } observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Result>() { @Override public void accept(Result result) throws Exception { if (result.isSuccess()) {// JSONObject obj = result.getResult();// String user = obj.getJSONArray("user_list").getString(0);// HashMap<String, Object> map = GsonUtils.fromJson(user, mapToken.getType());// tv_userid.setText(map.get("user_id").toString());// tv_userinfo.setText(map.get("user_info").toString());// tv_score.setText(map.get("score").toString()); } else { showToat("code:" result.getCode() ",msg:" result.getErrorMsg()); } tv_message.setText(result.getResponse()); log.d("耗时:" (System.currentTimeMillis() - start)); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { throwable.printStackTrace(); showToat("异常错误"); } }); } private void showToat(String msg) { Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); log.d(msg); } private String getLivesControl() { switch (livesRadioGroup.getCheckedRadioButtonId()) { case R.id.radio_lives_none: return "NONE"; case R.id.radio_lives_low: return "LOW"; case R.id.radio_lives_normal: return "NORMAL"; case R.id.radio_lives_high: return "HIGH"; default: return "NONE"; } } private String getQualityControl() { switch (qualityRadioGroup.getCheckedRadioButtonId()) { case R.id.radio_lives_none: return "NONE"; case R.id.radio_lives_low: return "LOW"; case R.id.radio_lives_normal: return "NORMAL"; case R.id.radio_lives_high: return "HIGH"; default: return "NONE"; } }}
下载百度人脸识别示例源码(android app)用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论